package com.sunnyebuy.merchant.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.sunnyebuy.merchant.dao.CustomerOrderDAO;
import com.sunnyebuy.merchant.dao.MerchantBaseDAO;
import com.sunnyebuy.merchant.po.CustomerOrder;
import com.sunnyebuy.merchant.status.PaymentStatus;
import com.sunnyebuy.merchant.status.ShipStatus;

/**
 * <p>
 * Title: CustomerOrderDAOImpl.java
 * </p>
 * 
 * <p>
 * Description: com.sunnyebuy.merchant.dao.impl
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2008-2011 All rights reserved.
 * </p>
 * 
 * <p>
 * Created: 2011-12-28 上午11:12:16
 * </p>
 * 
 * @author Wenzhong Gu
 * 
 * @version 1.0
 */
@Repository(value = "customerOrderDAO")
public class CustomerOrderDAOImpl extends MerchantBaseDAO implements CustomerOrderDAO {

	@Override
	@SuppressWarnings("unchecked")
	public List<CustomerOrder> findCustomerOrderByPaymentStatus(final String merchantId,
			final PaymentStatus paymentStatus) {
		return this.getHibernateTemplate().execute(new HibernateCallback<List<CustomerOrder>>() {
			@Override
			public List<CustomerOrder> doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "FROM CustomerOrder model WHERE model.merchantId=:merchantId AND model.paymentStatus=:paymentStatus ORDER BY model.customerOrderId";
				return session.createQuery(hql).setParameter("merchantId", merchantId)
						.setParameter("paymentStatus", paymentStatus).list();
			}
		});
	}

	@Override
	public void addCustomerOrder(final CustomerOrder customerOrder) throws Exception {
		this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				session.persist(customerOrder);
				return null;
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunnyebuy.merchant.dao.CustomerOrderDAO#findLastOrderTx()
	 */
	@Override
	public String getLastOrderTx() throws Exception {
		return this.getHibernateTemplate().execute(new HibernateCallback<String>() {
			public String doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "SELECT MAX(model.customerOrderTx) from  CustomerOrder model";
				Object obj = session.createQuery(hql).uniqueResult();
				if (obj != null) {
					return obj.toString();
				} else {
					return null;
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunnyebuy.merchant.dao.CustomerOrderDAO#findCustomerOrderByFulfillmentStatus(java.lang.String,
	 * com.sunnyebuy.merchant.status.ShipStatus)
	 */
	@Override
	public List<CustomerOrder> findCustomerOrderByShipmentStatus(final String merchantId, final ShipStatus shipStatus) {
		return this.getHibernateTemplate().execute(new HibernateCallback<List<CustomerOrder>>() {
			@SuppressWarnings("unchecked")
			public List<CustomerOrder> doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "FROM CustomerOrder model WHERE model.shippingStatus=:shipStatus AND model.merchantId=:merchantId ORDER BY model.customerOrderId DESC";
				return session.createQuery(hql).setParameter("merchantId", merchantId)
						.setParameter("shipStatus", shipStatus).list();
			}
		});
	}

}
